import hashlib
from datetime import time

from flask import render_template, jsonify, request, session, redirect, url_for
from . import index_blu
from models import db
from models.index import User, Blogs


@index_blu.route("/")
def about():
    email = session.get('email')
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('index.html', email=email, blogs_lists=blogs_lists)


@index_blu.route("/about.html")
def about1():
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('about.html', blogs_lists=blogs_lists)


@index_blu.route("/blog.html")
def blog():
    blogs_list = db.session.query(Blogs).order_by(-Blogs.update_time).all()
    email = session.get('email')
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('blog.html', email=email, blogs_list=blogs_list, blogs_lists=blogs_lists)


@index_blu.route("/blog-single.html")
def blog_single():
    blog_id = request.args.get('id')
    print('============', blog_id)
    email = session.get('email')
    blogs = db.session.query(Blogs).filter(Blogs.id == blog_id).first()
    return render_template('blog-single.html', email=email, blogs=blogs)


@index_blu.route("/contact.html")
def contact():
    email = session.get('email')
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('contact.html', email=email, blogs_lists=blogs_lists)


@index_blu.route("/gallery.html")
def gallery():
    email = session.get('email')
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('gallery.html', email=email, blogs_lists=blogs_lists)


@index_blu.route("/index.html")
def index():
    email = session.get('email')
    blogs_list = db.session.query(Blogs).order_by(-Blogs.update_time).limit(3)
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('index.html', email=email, blogs_list=blogs_list, blogs_lists=blogs_lists)


@index_blu.route("/main.py")
def main():
    return render_template('main.html')


@index_blu.route("/pricing.html")
def pricing():
    email = session.get('email')
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('pricing.html', email=email, blogs_lists=blogs_lists)


@index_blu.route("/services.html")
def services():
    email = session.get('email')
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('services.html', email=email, blogs_lists=blogs_lists)


@index_blu.route("/vet.html")
def vet():
    email = session.get('email')
    blogs_lists = db.session.query(Blogs).order_by(-Blogs.update_time).limit(2)
    return render_template('vet.html', email=email, blogs_lists=blogs_lists)


@index_blu.route('/login.html')
def user():
    return render_template('login.html')


@index_blu.route('/regiter.html')
def reg():
    return render_template('regiter.html')


@index_blu.route('/change_password.html')
def change_password():
    return render_template('change_password.html')


@index_blu.route('/user_blog.html')
def user_blog():
    return render_template('user_blog.html')


@index_blu.route("/passport/logout")
def logout():
    # 清空登录状态
    session.clear()

    return redirect(url_for('index_blu.about'))


@index_blu.route('/register', methods=["POST"])
def register():
    # 1.提取数据
    email = request.json.get('email')
    password = request.json.get('password')
    # 查看邮箱,密码
    print(email, password)
    # 2.创建一个新的用户
    # 2.1 先查询是否已有相同的用户
    if db.session.query(User).filter(User.e_mail == email).first():
        return '已经注册'
    # 2.2 注册用户
    # 将新用户的数据插入到数据库
    user = User()
    user.e_mail = email
    user.password_hash = password

    try:

        db.session.add(user)
        db.session.commit()

        ret = {
            'errno': 0,
            'errmsg': '成功'
        }
        return jsonify(ret)
    except Exception as ret:
        print("---->", ret)
        db.session.rollback()  # 如果在将用户的信


@index_blu.route("/login", methods=["GET", "POST"])
def login():
    # 1. 提取登录时的用户名，密码
    email = request.json.get("email")
    password = request.json.get("password")
    print(email, password)
    # 2. 查询，如果存在表示登录成功，否则失败
    user = db.session.query(User).filter(User.e_mail == email, User.password_hash == password).first()
    if user:
        ret = {
            "errno": 0,
            "errmsg": "登录成功"
        }
        session['user_id'] = user.id
        session['email'] = email
    else:
        ret = {
            "errno": 2001,
            "errmsg": "用户名或者密码错误"
        }

    return jsonify(ret)


@index_blu.route('/change_password_info', methods=['GET', 'POST'])
def change_password_info():
    # 1. 提取旧密码以及新密码
    email = request.json.get('email')
    old_password = request.json.get("password")
    new_password = request.json.get("password2")

    # 2. 判断邮箱是否存在密码是否正确
    user = db.session.query(User).filter(User.e_mail == email).first()

    # 3. 如果相同，则修改
    if user.password_hash == old_password:
        user.password_hash = new_password
        db.session.commit()
        ret = {
            "errno": 0,
            "errmsg": "修改成功"
        }

    else:
        ret = {
            "errno": 4004,
            "errmsg": "原密码错误！"
        }

    # 4. 返回json
    return jsonify(ret)


@index_blu.route('/user_blog', methods=['POST'])
def user_blog_info():
    title = request.json.get("input_txt2")
    digest = request.json.get("input_multxt")
    content = request.json.get("input_multxt2")
    f = request.files.get('input_file')

    print(title, digest, content, f)
    blogs = Blogs()
    blogs.title = title
    blogs.source = '个人发布'
    blogs.digest = digest
    blogs.content = content
    blogs.user_id = session.get('user_id')
    blogs.index_image_url = f
    db.session.add(blogs)
    db.session.commit()
    ret = {
        "errno": 0,
        "errmsg": "成功"
    }

    return jsonify(ret)
